#!/usr/bin/env python
# encoding: utf-8
"""
@author:zwb
@file: archives.py
@contact: zwb15083976291@163.com
@time: 2022/02/24
"""
from common.models import BlogArticle
from common.serializers.serializer import ArticleArchivesSerializer
from utils.filters.views_filters import BaseSearchFilter
from utils.responses import CommonResponse, CommonAPIResponseStatus
from utils.serializers.serializer import PaginatorSerializer
from utils.views import BasePaginateGenericAPIView


class ArchivesListView(BasePaginateGenericAPIView):
    diff_methods_serializer_class = dict(get=PaginatorSerializer)
    diff_methods_request_params = dict(
        get=dict(optional=['pageNum', 'pageSize'],
                 defualt_value=dict(pageNum=1, pageSize=5)))
    filter_backends = [BaseSearchFilter]

    def get(self, request):
        ret, message = self.validate_request_params()
        if not ret:
            return CommonResponse(CommonAPIResponseStatus.BAD_REQUEST, message=message)
        serial_fields = ['id', 'create_time', 'articles_title']
        query_set = self.filter_queryset(
            queryset=BlogArticle.objects.filter(articles_status=0).order_by('-create_time'))
        paginator, models = self.paginate_queryset(query_set, self.serializer.validated_data)
        return CommonResponse(data=self.get_paginated_response(self.serializer.validated_data, paginator,
                                                               ArticleArchivesSerializer(instance=models,
                                                                                         serial_fields=serial_fields,
                                                                                         many=True).data))
